/**
 * FileName: AirBookingStatisticsController
 * Author:   Leopold
 * Date:     2018/8/24 14:54
 * Description: 航展车辆预约统计
 * History:
 */
package cn.mcsys.myadmin.modules.air.web;

import cn.mcsys.myadmin.modules.air.dao.AirRideQrCodeSpecialDao;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.air.entity.AirRideQrCodeSpecial;
import cn.mcsys.myadmin.modules.air.service.AirCarRecordService;
import cn.mcsys.myadmin.modules.air.service.AirRidePointService;
import cn.mcsys.myadmin.modules.viewer.service.AirRideQRCodeService;
import com.alibaba.fastjson.JSON;
import com.github.abel533.echarts.*;
import com.github.abel533.echarts.axis.Axis;
import com.github.abel533.echarts.axis.AxisLabel;
import com.github.abel533.echarts.axis.CategoryAxis;
import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.PointerType;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.series.Series;
import com.github.abel533.echarts.style.ItemStyle;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("${adminPath}/air/bookingStatistics")
public class AirBookingStatisticsController {

    @Autowired
    private AirRidePointService airRidePointService;

    @Autowired
    private AirCarRecordService airCarRecordService;

    @Autowired
    private AirRideQRCodeService airRideQRCodeService;

    @Autowired
    private AirRideQrCodeSpecialDao airRideQrCodeSpecialDao;


    @RequestMapping(value = "index")
    @RequiresPermissions("air:bookingStatistics:view")
    public String index(Model model,String bookingDate) {
        if(bookingDate == null){
            bookingDate = "2018-11-09";
        }
        List<AirRidePoint> allList = airRidePointService.findAllPointList();
        Collections.reverse(allList);
        List<String> pointNames = new ArrayList<>();
        List<Integer> bookingCounts = new ArrayList<>();
        List<Integer> arriveCounts = new ArrayList<>();
        Map<AirRidePoint,Integer> map = new HashMap<>();

//        算出各个乘车点的预约数,预约数和运送人数个算出来
//        通过计算二维码表进行的
//        查询的是9号,10号,11号
//        刚进来默认是9号的
        for (AirRidePoint airRidePoint : allList) {
            pointNames.add(airRidePoint.getRoute()+" "+airRidePoint.getName());
//            各个乘车点的运送人数(日期)
            Integer sum = airRideQRCodeService.selectCountByPointId(airRidePoint.getId(),bookingDate);
            if(sum==null){
                sum = 0;
            }
            bookingCounts.add(sum);
//            查询已运送的人数
            Integer arriveSum = airCarRecordService.arriveSum(airRidePoint.getName(), bookingDate);
            List<AirRideQrCodeSpecial> rideQrCodeSpecials = airRideQrCodeSpecialDao.getQrCodeSpecialByNote(String.valueOf(airRidePoint.getId()));
            for (AirRideQrCodeSpecial special : rideQrCodeSpecials) {
                arriveSum += special.getRideNo();
            }
            arriveCounts.add(arriveSum);
            map.put(airRidePoint,sum);
        }

        Comparator<Map.Entry<AirRidePoint, Integer>> valueComparator = new Comparator<Map.Entry<AirRidePoint,Integer>>() {
            @Override
            public int compare(Map.Entry<AirRidePoint, Integer> o1,
                               Map.Entry<AirRidePoint, Integer> o2) {
                if(o1.getKey().getRoute()>0) {
                    return o1.getKey().getRoute() - o2.getKey().getRoute();
                }else {
                    return -1;
                }
            }
        };
//         map转换成list进行排序
        List<Map.Entry<AirRidePoint, Integer>> list = new ArrayList<Map.Entry<AirRidePoint,Integer>>(map.entrySet());
//         排序
        Collections.sort(list,valueComparator);

        model.addAttribute("bookingDate",bookingDate);
        model.addAttribute("map",list);
        model.addAttribute("arriveCounts",JSON.toJSON(arriveCounts));
        model.addAttribute("pointNames", JSON.toJSON(pointNames));
        model.addAttribute("counts", JSON.toJSON(bookingCounts));
        return "modules/air/airBookingStatistics";
    }

    @RequestMapping(value = "getOption")
    @ResponseBody
    public Option getOption() {
        //        查询所有乘车点的时间段的对应
//        List<AirRidePoint> list = airRidePointService.findAllList();
        List<AirRidePoint> list = airRidePointService.findAllAppointedList();

//        GsonOption option = new GsonOption();
        Option option = new Option();

        Tooltip tooltip = new Tooltip();
        tooltip.setTrigger(Trigger.axis);
        AxisPointer axisPointer = new AxisPointer();
        axisPointer.setType(PointerType.shadow);

        tooltip.setAxisPointer(axisPointer);
        option.setTooltip(tooltip);

        Grid grid = new Grid();
        grid.left(100);
        option.setGrid(grid);

        Toolbox toolbox = new Toolbox();
        toolbox.show(true);

        //x轴
        Axis xAxis = new ValueAxis();
        xAxis.setType(AxisType.value);
        xAxis.setName("人数");
        AxisLabel axisLabel = new AxisLabel();
        axisLabel.setFormatter("{value}");
        xAxis.setAxisLabel(axisLabel);
        option.setxAxis(Collections.singletonList(xAxis));

        //Y轴  valueAxis.
        Axis yAxis = new CategoryAxis();
        yAxis.setType(AxisType.category);
        yAxis.setInverse(true);
        List<String> data = setData(list);
        yAxis.setData(data);

        AxisLabel axisLabel1 = new AxisLabel();
        axisLabel1.setMargin(20);
        yAxis.setAxisLabel(axisLabel1);
        option.setyAxis(Collections.singletonList(yAxis));

        Legend legend = new Legend();
        List<String> points = new ArrayList<>();

        List<Series> series = new ArrayList<>();
        List<Integer> array = new ArrayList<>();
        for (AirRidePoint airRidePoint : list) {
            Series bar = new Bar();
            bar.setType(SeriesType.bar);
            bar.setName(airRidePoint.getName());
            int sum = airRidePoint.getBookSeven() + airRidePoint.getBookSevenH() + airRidePoint.getBookEight() + airRidePoint.getBookEightH() + airRidePoint.getBookNine() + airRidePoint.getBookNineH()
                    + airRidePoint.getBookTen() + airRidePoint.getBookTenH() + airRidePoint.getBookEleven() + airRidePoint.getBookElevenH();
            points.add(airRidePoint.getName());
            array.add(sum);
            bar.setData(array);
            series.add(bar);
        }
        legend.setData(points);
        option.setLegend(legend);
        option.series(series);
        option.setColor(setColor());
        return option;
    }


    private List<String> setData() {
        ArrayList<String> list = new ArrayList<>();
        list.add("7:00-7:30");
        list.add("7:30-8:00");
        list.add("8:00-8:30");
        list.add("8:30-9:00");
        list.add("9:00-9:30");
        list.add("9:30-10:00");
        list.add("10:00-10:30");
        list.add("10:30-11:00");
        list.add("11:00-11:30");
        list.add("11:30-12:00");
        return list;
    }

    private List<Object> setColor() {
        ArrayList<Object> list = new ArrayList<>();
        list.add("#ff0000");
        list.add("#ff9900");
        list.add("#ffff00");
        return list;
    }

    private List<String> setData(List<AirRidePoint> list) {
        ArrayList<String> result = new ArrayList<>();
        for (AirRidePoint airRidePoint : list) {
            result.add(airRidePoint.getName());
        }
        return result;
    }

    @RequestMapping(value = "getOption2")
    @ResponseBody
    public Option getOption2() {
        //        查询所有乘车点的时间段的对应
        List<AirRidePoint> list = airRidePointService.findAllList();

        Option option = new Option();

        Tooltip tooltip = new Tooltip();
        tooltip.setTrigger(Trigger.axis);
        AxisPointer axisPointer = new AxisPointer();
        axisPointer.setType(PointerType.shadow);

        tooltip.setAxisPointer(axisPointer);
        option.setTooltip(tooltip);

        Grid grid = new Grid();
        grid.left(100);
        option.setGrid(grid);

        Toolbox toolbox = new Toolbox();
        toolbox.show(true);

        Axis xAxis = new ValueAxis();
        xAxis.setType(AxisType.value);
        xAxis.setName("人数");
        AxisLabel axisLabel = new AxisLabel();
        axisLabel.setFormatter("{value}");
        xAxis.setAxisLabel(axisLabel);
        option.setxAxis(Collections.singletonList(xAxis));

        Axis yAxis = new CategoryAxis();
        yAxis.setType(AxisType.category);
        yAxis.setInverse(true);
        List<String> data = setData(list);
        yAxis.setData(data);

        AxisLabel axisLabel1 = new AxisLabel();
        axisLabel1.setMargin(20);
        yAxis.setAxisLabel(axisLabel1);
        option.setyAxis(Collections.singletonList(yAxis));

        Legend legend = new Legend();
        List<String> points = new ArrayList<>();
        List<Series> series = new ArrayList<>();
        Bar bar = new Bar();
        List<Integer> array = new ArrayList<>();
        int i = 0;
        for (AirRidePoint airRidePoint : list) {
            bar.setType(SeriesType.bar);
//            bar.setName(airRidePoint.getName());
            int sum = airRidePoint.getBookSeven() + airRidePoint.getBookSevenH() + airRidePoint.getBookEight() + airRidePoint.getBookEightH() + airRidePoint.getBookNine() + airRidePoint.getBookNineH()
                    + airRidePoint.getBookTen() + airRidePoint.getBookTenH() + airRidePoint.getBookEleven() + airRidePoint.getBookElevenH();
            array.add(sum);
            points.add(airRidePoint.getName());
            series.add(bar);
            ItemStyle itemStyle = new ItemStyle();
            itemStyle.normal().color("#ffff00");
            bar.setItemStyle(itemStyle);
            i++;
        }
        bar.setData(array);
        legend.setData(points);
        option.setLegend(legend);
        option.setColor(setColor());
        option.series(bar);

        return option;
    }

    @RequestMapping(value = "getData")
    @ResponseBody
    public List<AirRidePoint> getList() {
        return airRidePointService.findAllAppointedList();
    }
}
